//
//  QCHeightRulerCell.swift
//  爱汽车
//
//  Created by liujianlong on 2023/8/30.
//

import UIKit

class QCHeightRulerCell: UITableViewCell {
    @IBOutlet var title: UILabel!
    @IBOutlet  var bgView: UIView!
    @IBOutlet private var min: UILabel!
    @IBOutlet private var max: UILabel!
    private var lineLayer : CAShapeLayer?
    private var minX : CGFloat = 0
    private var maxX : CGFloat = 0
    var block:((_ value: Int)->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
        let pan = UIPanGestureRecognizer(target:self, action:#selector(actionForPan))
        self.addGestureRecognizer(pan)
    }
    var limit = (max:100,min:0){
        didSet{
            max.text = "\(limit.max)"
            min.text = "\(limit.min)"
        }
    }
    
    var value : Int = 0{
        didSet{
            guard lineLayer != nil else {return}
            let animation = CABasicAnimation(keyPath: "position.x")
            animation.fromValue = minX // 起始位置
            animation.toValue = CGFloat(value) / CGFloat(limit.max) * (maxX - minX - 1)// 终点位置
            animation.duration = 0.01 // 动画持续时间
            animation.fillMode = .forwards
            animation.isRemovedOnCompletion = false
            lineLayer!.add(animation, forKey: "positionXAnimation")
        }
    }
    
    var point : CGPoint = .zero{
        didSet{
            if point.y > bgView.bounds.height {return}
            let x = point.x <= minX ? 0 : point.x >= maxX ? maxX - minX - 1 : point.x - minX
            let scale = x / (maxX - minX - 1)
            value = Int(scale * CGFloat(limit.max))
//            block?(value)
        }
    }
    
    @objc func actionForPan(_ pan:UIPanGestureRecognizer){
        //point = pan.location(in: bgView)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
//        guard let touch = touches.first else { return}
//        point = touch.location(in: bgView)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        contentView.layoutIfNeeded()
        guard lineLayer == nil else {return}
        
        minX = min.frame.maxX + 10
        maxX = max.frame.minX - 10
        let y = min.frame.midY
        ///画刻度横线
        let p1 = CGPoint(x:minX, y:y)
        let p2 = CGPoint(x:maxX, y:y)
        _ = drawLayer(p1, p2)
        
        ///画刻度竖线
        let gap = (maxX - minX - 11.0) / 10.0
        for i in 0...10{
            let x = Double(i) * (gap + 1.0) + minX + 0.5
            let h = i == 0 || i == 10 ? 10.0 : 6.0
            let p1 = CGPoint(x:x, y: y)
            let p2 = CGPoint(x:x, y: y - h)
            _ = drawLayer(p1, p2, 1)
        }
        
        ///画选中的竖线
        let p3 = CGPoint(x:minX + 0.5, y: y + 10.0)
        let p4 = CGPoint(x:minX + 0.5, y: y - 10.0)
        lineLayer = drawLayer(p3, p4, 1,"#EA4904".color())
    }
    
    func drawLayer(_ p1:CGPoint, _ p2:CGPoint, _ w:CGFloat=2, _ color:UIColor="#FFFFFF".color())-> CAShapeLayer{
        let layer = CAShapeLayer()
        let path = UIBezierPath()
        path.move(to:p1)
        path.addLine(to:p2)
        layer.path = path.cgPath
        layer.lineWidth = w
        layer.strokeColor = color.cgColor
        bgView.layer.addSublayer(layer)
        return layer
    }
    
}
